package web.servlet;

import domain.Account;
import domain.Cart;
import domain.Product;
import service.AccountService;
import service.CartService;
import service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class SignOnServlet extends HttpServlet {

    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signOn.jsp";

    private String username;
    private String password;
    private String captcha;
    private String msg;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");
        this.captcha = req.getParameter("captcha");
        HttpSession session = req.getSession();
        String storedCaptcha = (String) session.getAttribute("captcha");

        //校验用户输入的正确性
        if (!validate()) {
            req.setAttribute("signOnMsg", this.msg);
            req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
        } else {

            AccountService accountService = new AccountService();
            //Account loginAccount = accountService.getAccountByNameAndPassword(username, password);
            Account loginAccount = accountService.getAccountByName(username);
            if (loginAccount == null) {
                req.setAttribute("signOnMsg", this.msg = "用户名不存在");
                req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
            } else {
                //用户名存在，判断密码是否正确
                if (this.captcha != null && this.captcha.equals(storedCaptcha)) {
                    // 验证码正确，继续处理登录
                    Account loginAccount2 = accountService.getAccountByNameAndPassword(username, password);
                    if (loginAccount2 == null) {
                        req.setAttribute("signOnMsg", this.msg = "密码错误");
                        req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
                    } else {
                        loginAccount2.setPassword(null);
                        session.setAttribute("loginAccount", loginAccount2);

                        if (loginAccount2.isListOption()) {
                            CatalogService catalogService = new CatalogService();
                            List<Product> myList = catalogService.getProductListByCategory(loginAccount.getFavouriteCategoryId());
                            session.setAttribute("myList", myList);
                        }
                        CartService cartService = new CartService();
                        Cart cart = (Cart) session.getAttribute("cart");
                        if (cart != null) {

                            cartService.insertListInCart(cart.getCartItemList(), loginAccount2.getUsername());
                        }
                        Cart cart2 = new Cart();
                        cart2.addItemFromSQL(cartService.getCartItemFromUserId(loginAccount2.getUsername()));
                        session.setAttribute("cart", cart2);
                        resp.sendRedirect("mainForm?from=signOnServlet");
                    }
                } else {
                    // 验证码错误
                    req.setAttribute("signOnMsg", this.msg = "验证码错误");
                    req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
                }
            }
        }
    }

    private boolean validate() {
        if (this.username == null || this.username.equals("")) {
            this.msg = "用户名不能为空";
            return false;
        }
        if (this.password == null || this.password.equals("")) {
            this.msg = "密码不能为空";
            return false;
        }
        return true;
    }
}